package com.suitsoft.bdms.commos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import com.google.common.collect.Lists;
import com.suitsoft.bdms.entity.account.Role;
import com.suitsoft.bdms.entity.account.SubSystem;

/**
 * 登录用户信息类.
 */

public class LoginUser extends User {
	/** 序列ID */
	private static final long serialVersionUID = 1L;
	/** 用户ID */
	private Long id;
	/** 用户登录名 */
	private String loginName;
	/** 密码 */
	private String password;// 为简化演示使用明文保存的密码
	/** 用户名 */
	private String name;
	/** 用户EMAIL */
	private String email;
	/** 用户角色 */
	private List<Role> roleList = new ArrayList<Role>();
	/** 系统 */
	private List<SubSystem> systemList = Lists.newArrayList();
	
	/** 分所代码 */
	private String officeCode;
	/** 分所名称 */
	private String officeName;
	/** 分所代码集合 */
	private Set<String> officeCodeSet;
	/** 字符串形式的分所代码集合，逗号分隔 */
	private String officeCodes;
	/** 当前用户可访问的目录树 */
	@SuppressWarnings("unchecked")
	private List tree;
	private Long systemId;
	
	public LoginUser(String username, String password, boolean enabled,
			boolean accountNonExpired, boolean credentialsNonExpired,
			boolean accountNonLocked,
			Collection<? extends GrantedAuthority> authorities) {
		super(username, password, enabled, accountNonExpired, credentialsNonExpired,
				accountNonLocked, authorities);
	}
	
	
	@SuppressWarnings("unchecked")
	public String getOfficeCodes() {
		if (this.officeCodes == null) {
			StringBuffer buffer = new StringBuffer();
			Iterator it = officeCodeSet.iterator();
			buffer.append("(");
			if (it.hasNext()) {
				buffer.append("'");
				buffer.append(it.next());
				buffer.append("'");
			}
			
			while(it.hasNext()) {
				buffer.append(", ");
				buffer.append("'");
				buffer.append(it.next());
				buffer.append("'");
			}
			buffer.append(")");
			officeCodes = buffer.toString();
		}
		
		return officeCodes;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@Override
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public String getOfficeCode() {
		return officeCode;
	}

	public void setOfficeCode(String officeCode) {
		this.officeCode = officeCode;
	}

	public String getOfficeName() {
		return officeName;
	}

	public void setOfficeName(String officeName) {
		this.officeName = officeName;
	}

	public Set<String> getOfficeCodeSet() {
		return officeCodeSet;
	}

	public void setOfficeCodeSet(Set<String> officeCodeSet) {
		this.officeCodeSet = officeCodeSet;
	}


	public void setOfficeCodes(String officeCodes) {
		this.officeCodes = officeCodes;
	}

	@SuppressWarnings("unchecked")
	public List getTree() {
		return tree;
	}

	@SuppressWarnings("unchecked")
	public void setTree(List tree) {
		this.tree = tree;
	}


	public List<SubSystem> getSystemList() {
		return systemList;
	}


	public void setSystemList(List<SubSystem> systemList) {
		this.systemList = systemList;
	}


	public Long getSystemId() {
		return systemId;
	}

	public void setSystemId(Long systemId) {
		this.systemId = systemId;
	}
}
